/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Correction.java
 *
 * Created on 16 mars 2011, 23:25:43
 */

package jade.ui;

import jade.ia.Moteur;
import jade.model.Entite;
import jade.model.QuestionEntite;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * Fenêtre invitant l'utilisateur a saisir une correction
 */
public class Correction extends javax.swing.JFrame {

    // Moteur du jeu
    private Moteur moteur;

    // On retient la fenêtre parente pour gérer le comportement d'une modale
    private JFrame proposition;

    /** Creates new form Correction */
    public Correction(Moteur moteur, JFrame proposition) {
        initComponents();
        this.moteur = moteur;
        this.proposition = proposition;
        
        this.setLocationRelativeTo(this.getParent());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        btnEnregistrer = new javax.swing.JButton();
        chkNon = new javax.swing.JRadioButton();
        chkOui = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        txtNouvelleQuestion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtVraiReponse = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnAnnuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Apprenez-moi la solution !");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        btnEnregistrer.setText("Enregistrer");
        btnEnregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnregistrerActionPerformed(evt);
            }
        });

        chkNon.setText("Non");

        chkOui.setText("Oui");

        jLabel8.setText("Votre proposition correspond à quel choix de la question ?");

        jLabel7.setText("Nouvelle question");

        jLabel6.setText("A quoi pensiez vous ?");

        txtVraiReponse.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtVraiReponseFocusLost(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Je me suis trompé cette fois ci, mais apprenez-moi à être meilleur !");

        btnAnnuler.setText("Annuler");
        btnAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnnulerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel7)
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtNouvelleQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtVraiReponse, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addGap(13, 13, 13)
                            .addComponent(chkOui)
                            .addGap(18, 18, 18)
                            .addComponent(chkNon)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAnnuler)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 289, Short.MAX_VALUE)
                        .addComponent(btnEnregistrer)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtVraiReponse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNouvelleQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(chkOui))
                    .addComponent(chkNon))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEnregistrer)
                    .addComponent(btnAnnuler))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Enregistrer la correction
     * @param evt
     */
    private void btnEnregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnregistrerActionPerformed
        String reponse = this.txtVraiReponse.getText();
        String question = this.txtNouvelleQuestion.getText();
        Boolean oui = this.chkOui.isSelected();
        Boolean non = this.chkNon.isSelected();

        if ("".equals(reponse)||"".equals(question)|| !(oui||non)) {
            JOptionPane.showMessageDialog(this, "Merci de remplir le formulaire correctement !", "Erreur de saisie", JOptionPane.ERROR_MESSAGE);
        } else {

            Boolean res = moteur.apprendreNouvelleReponse(question, reponse, oui);
            if (!res) JOptionPane.showMessageDialog(this, "Une erreur est survenue durant l'apprentissage de cette réponse", "Erreur", JOptionPane.ERROR_MESSAGE);
            else JOptionPane.showMessageDialog(this, "Merci pour votre contribution !");

            this.dispose();
        }
}//GEN-LAST:event_btnEnregistrerActionPerformed

    private void txtVraiReponseFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtVraiReponseFocusLost
        String reponse = this.txtVraiReponse.getText();
        if (!"".equals(reponse)) {
            Entite entiteCorrespondante = moteur.verifierExistence(reponse);

            // Cette entité existe-t'elle déjà ?
            if (entiteCorrespondante != null) {
                QuestionEntite qBiff = moteur.rechercherBifurcation(entiteCorrespondante, moteur.getProposition());
                JOptionPane.showMessageDialog(this, "L'entité \""+reponse+"\" existe déjà ! \nVous n'avez pas choisi la bonne réponse à la question :\n >> "+qBiff.getQuestion(), "Erreur de parcours", JOptionPane.ERROR_MESSAGE);

                this.dispose();
            }
        }
}//GEN-LAST:event_txtVraiReponseFocusLost

    private void btnAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnnulerActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnAnnulerActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        proposition.dispose();
    }//GEN-LAST:event_formWindowClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnnuler;
    private javax.swing.JButton btnEnregistrer;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton chkNon;
    private javax.swing.JRadioButton chkOui;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField txtNouvelleQuestion;
    private javax.swing.JTextField txtVraiReponse;
    // End of variables declaration//GEN-END:variables

}
